
#include "..\include\LinkedList.h"

#include <memory>

LinkedList::LinkedList()
{
	this->node = nullptr;
}

void LinkedList::put(int number)
{

	if (!this->node)
	{
		this->node = (Node*)malloc(sizeof(Node));

		this->node->number = number;

		this->node->next = nullptr;
		return;
	}

	Node original = *this->node;

	this->node->number = number;

	this->node->next = (Node*)malloc(sizeof(Node));
	
	*this->node->next = original;
}

void LinkedList::print()
{
	if (!this->node)
	{
		return;
	}

	cout << "number: " << this->node->number << endl;

	Node* node = this->node->next;

	while (node) {
		cout << "next:" << node->number << endl;
		node = node->next;
	}

	node = nullptr;

}

void LinkedList::del(int n)
{
	Node* head = this->node;

	Node* next = head->next;


	while (nullptr != next) {
	
		if (next->number == n) {

			head->next = next->next;
			
			free(next);
			
			next = head->next;
			
		} else {

			head = head->next;

			next = next->next;
		}
	}
}

LinkedList::~LinkedList()
{
	delete this->node;
}
